﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.ComponentModel;

namespace DAL
{
    /// <summary>
    /// Summary description for ProductImage
    /// </summary>
    public partial class ProductImage : INotifyPropertyChanging, INotifyPropertyChanged
    {
        public void SetUrl(HttpPostedFile httpPostedFile, string physicalApplicationPath)
        {
            string dir = string.Format("{0}Images\\Products\\", physicalApplicationPath);
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);
            dir = string.Format("{0}Images\\Products\\{1}", physicalApplicationPath, this.ProductId);
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);
            httpPostedFile.SaveAs(string.Format("{0}\\{1}.jpg", dir, this.ImageId));
            this.Url = string.Format("images/Products/{0}/{1}.jpg", this.ProductId, this.ImageId);
        }

        public void AddImageToProduct(int productId, string physicalApplicationPath, HttpPostedFile httpPostedFile)
        {
            if (Roles.IsUserInRole("Administrator"))
            {
                if (httpPostedFile != null)
                {
                    if (httpPostedFile.ContentType != "image/pjpeg" && httpPostedFile.ContentType != "image/gif" && httpPostedFile.ContentType != "image/jpeg")
                        return;
                }
                else
                    return;
                using (DAL.EShopDBDataContext db = new DAL.EShopDBDataContext())
                {
                    var product = db.Products.Single(c => c.ProductId == productId);
                    this.Url = "";
                    product.ProductImages.Add(this);
                    db.SubmitChanges();
                    if (product.ProductImages.Count == 1)
                        product.DefaultImageId = this.ImageId;
                    SetUrl(httpPostedFile, physicalApplicationPath);
                    db.SubmitChanges();
                }
            }
        }

        public static ProductImage[] SelectProductImages(int productId)
        {
            using (DAL.EShopDBDataContext db = new EShopDBDataContext())
            {
                var pImages = from p in db.ProductImages
                              where p.ProductId == productId
                              select p;
                return pImages.ToArray();
            }
        }

        public static void InsertProductImage(int productId, string physicalApplicationPath, HttpPostedFile httpPostedFile)
        {
            if (Roles.IsUserInRole("Administrator"))
            {
                if (httpPostedFile != null)
                {
                    if (httpPostedFile.ContentType != "image/pjpeg" && httpPostedFile.ContentType != "image/gif" && httpPostedFile.ContentType != "image/jpeg")
                        return;
                }
                else
                    return;
                using (DAL.EShopDBDataContext db = new DAL.EShopDBDataContext())
                {
                    using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
                    {
                        var product = db.Products.Single(c => c.ProductId == productId);
                        ProductImage pi = new ProductImage { Url = "" };
                        product.ProductImages.Add(pi);
                        db.SubmitChanges();
                        if (product.ProductImages.Count == 1)
                            product.DefaultImageId = pi.ImageId;
                        SetProductImageUrl(httpPostedFile, physicalApplicationPath, pi);
                        db.SubmitChanges();
                        ts.Complete();
                    }
                }
            }
        }

        private static void SetProductImageUrl(HttpPostedFile httpPostedFile, string physicalApplicationPath, ProductImage pi)
        {
            string dir = string.Format("{0}Images\\Products\\", physicalApplicationPath);
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);
            dir = string.Format("{0}Images\\Products\\{1}", physicalApplicationPath, pi.ProductId);
            if (!System.IO.Directory.Exists(dir))
                System.IO.Directory.CreateDirectory(dir);
            httpPostedFile.SaveAs(string.Format("{0}\\{1}.jpg", dir, pi.ImageId));
            pi.Url = string.Format("images/Products/{0}/{1}.jpg", pi.ProductId, pi.ImageId);
        }

        public static string GetDefaultProductImageUrl(int productId)
        {
            using (DAL.EShopDBDataContext db = new EShopDBDataContext())
            {
                var pi = db.ProductImage_GetDefaultProductImage(productId).Single();
                return pi.Url;
            }
        }
    }
}
